package com.i8office.android.zhihuixinwu.util;

import android.content.Context;
import android.graphics.Point;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.view.WindowManager;

import com.zejian.emotionkeyboard.utils.DisplayUtils;

import application.AppApplication;

public class ViewUtil {
    /**
     * 根据Tab合起来的长度动态修改tab的模式
     *
     * @param tabLayout TabLayout
     */
    public static void dynamicSetTabLayoutMode(TabLayout tabLayout, int leftMargin, int rifhtMargin) {
        int tabTotalWidth = 0;
        for (int i = 0; i < tabLayout.getChildCount(); i++) {
            final View view = tabLayout.getChildAt(i);
            view.measure(0, 0);
            tabTotalWidth += view.getMeasuredWidth();
        }
        if (tabTotalWidth <= getScreenSize(tabLayout.getContext()).x - DisplayUtils.dp2px(AppApplication.getContext(), leftMargin) - DisplayUtils.dp2px(AppApplication.getContext(), rifhtMargin)) {
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            tabLayout.setTabMode(TabLayout.MODE_FIXED);
        } else {
            tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        }
    }

    public static Point getScreenSize(Context context) {

        // 获取屏幕宽高
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point screenSize = new Point();
        wm.getDefaultDisplay().getSize(screenSize);
        return screenSize;
    }
}
